<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="字典类型" prop="dictType">
      <el-input v-model="dataForm.dictType" placeholder="字典类型"></el-input>
    </el-form-item>
    <el-form-item label="字典名称" prop="dictName">
      <el-input v-model="dataForm.dictName" placeholder="字典名称"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dictType: '',
          dictName: '',
          remark: '',
          sort: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          deleteTag: ''
        },
        dataRule: {
                  dictType: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
              dictName: [
            { required: true, message: '字典名称不能为空', trigger: 'blur' }
          ],
                        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdicttype/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.dictType = data.data.dictType
                this.dataForm.dictName = data.data.dictName
                this.dataForm.remark = data.data.remark
                this.dataForm.sort = data.data.sort
                this.dataForm.createTime = data.data.createTime
                this.dataForm.updateId = data.data.updateId
                this.dataForm.updateTime = data.data.updateTime
                this.dataForm.deleteTag = data.data.deleteTag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/sysdicttype/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `post`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dictType': this.dataForm.dictType,
                'dictName': this.dataForm.dictName,
                'remark': this.dataForm.remark,
                'sort': this.dataForm.sort,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'deleteTag': this.dataForm.deleteTag
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
